package com.myproject.music.controller;


import com.myproject.music.BizException;
import com.myproject.music.domain.Song;
import com.myproject.music.service.SongService;
import com.myproject.music.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/song")
@Api(tags = "歌曲管理0515郭彦良")
public class SongController
{
    @Autowired
    private SongService songService;

    @ApiOperation("查询歌曲列表")
    @ApiImplicitParam(name="name", value="歌名", dataType="string", dataTypeClass=String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String songName) {
        List<Song> list = songService.selectSongByParam(songName);
        return AjaxResult.success(list);
    }
    @ApiOperation("查询歌曲列表")
    @ApiImplicitParam(name="name", value="歌手名字", dataType="string", dataTypeClass=String.class)
    @GetMapping("/list1")
    public AjaxResult list1(@RequestParam(value = "name", required = false) String creator) {
        List<Song> list = songService.selectCreatorByParam(creator);
        return AjaxResult.success(list);
    }

    @ApiOperation(value="获取歌曲详细信息", notes = "根据歌曲ID获取详细信息")
    @ApiImplicitParam(name = "id", value="歌曲ID", required=true, dataType = "long", paramType="path", dataTypeClass=Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Song song1=songService.selectSongById(id);
        if(song1 ==null||song1.getId()==null){
            throw new BizException(404,"歌曲"+id+"不存在");
        }
        return AjaxResult.success(song1);
    }
    // @Log(title = "歌手管理",businessType = BusinessType.INSERT)

    @ApiOperation("新增歌曲")

    @PostMapping("/insertsong")
    public AjaxResult add( @RequestBody Song song) {
        return AjaxResult.success(songService.insertSong(song));
    }

    @ApiOperation("修改歌曲")
    @PutMapping
    public AjaxResult edit(@RequestBody Song song) {
        return AjaxResult.success(songService.updateSong(song));
    }

    @ApiOperation("删除歌曲")
    @ApiImplicitParam(name="id", value = "歌曲ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(songService.deleteSongById(id));
    }

    @ApiOperation("查询全部歌曲列表")
    @GetMapping("/songlist")
    public AjaxResult getAllSongList() {
        return AjaxResult.success(songService.selectSongAll());
    }
}
